﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.XPath;

namespace SharingCenter.Web.Admin
{
    using SharingCenter.Web.Classes;
    using System.Xml.Linq;
    public partial class MessageSettings : AdminPageBase
    {
        static string[] args = null;
        static XDocument x = null;
        protected void Page_Init(object sender, EventArgs e)
        {
            LinqDataSource1.Selected += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Selected);
            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
                //FormView1.DataBound += new EventHandler(FormView1_DataBound);
                //lvMailParam.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lvMailParam_ItemDataBound);
            lvMailParam.DataBinding += new EventHandler(lvMailParam_DataBinding);
            
            FormView1.ItemInserting += new FormViewInsertEventHandler(FormView1_ItemInserting);
            FormView1.PreRender += new EventHandler(FormView1_PreRender);
        }

        void FormView1_PreRender(object sender, EventArgs e)
        {
            //args = null;
        }

        void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            e.Values["MailTemplateId"] = Guid.NewGuid();
            e.Values["MailEventGroup"] = args[0];
            e.Values["MailEventName"] = args[1];
        }

        void lvMailParam_DataBinding(object sender, EventArgs e)
        {
            if (args != null)
            {
                

                //XmlDataSource2.XPath = string.Format(@"//EventGroup[@EventGroupName='{0}']/Event[@EventName='{1}']//Parameter", args[0], args[1]);
            }
        }

        void lvMailParam_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ListViewDataItem item = (ListViewDataItem)e.Item;
            //e.Item.FindControl("lnkParam")
        }

        void FormView1_DataBound(object sender, EventArgs e)
        {
            
        }

        protected void btnEventName_Load(object sender, EventArgs e)
        {
            AjaxScriptManager.GetCurrent(this).RegisterAsyncPostBackControl((Control)sender);
        }

        void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
        {
            if (args != null)
            {
                FormView1.Visible = true;
                List<Sc_MailTemplate> tlist = (List<Sc_MailTemplate>)e.Result;
                if (tlist.Count == 0)
                {
                    FormView1.ChangeMode(FormViewMode.Insert);
                }
                else
                {
                    FormView1.ChangeMode(FormViewMode.Edit);
                }
            }
            else
            {
                FormView1.Visible = false;
            }
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (args != null)
            {
                e.WhereParameters["MailEventGroup"] = args[0];
                e.WhereParameters["MailEventName"] = args[1];
            }
            //e.Result = from t in scDataContext.Instance.Sc_MailTemplates
            //           where t.MailEventGroup == args[0] && t.MailEventName == args[1]
            //           select t;
        }

        protected void btnEventName_Command(object sender, CommandEventArgs e)
        {
            char[] s = { '|' };
            args = e.CommandArgument.ToString().Split(s);
            
            ShowForms(true);
        }

        void ShowForms(bool show)
        {
            try
            {
                lblEventDesc.Text = args[2];
            }
            catch { }

            pnForm.Visible = show;
            pnParam.Visible = show;
            //LinqDataSource1.DataBind();
            FormView1.DataBind();
            BindParam();
        }

        void BindParam()
        {
            if (args != null)
            {
                string xpath = string.Format(@"//EventGroup[@EventGroupName='{0}']/Event[@EventName='{1}']//Parameter", args[0], args[1]);
                x = XDocument.Load(Server.MapPath(@"~/App_Data/MessageReplacementKey.xml"));
                
                lvMailParam.DataSourceID = null;
                var q = from t in x.XPathSelectElements(xpath)
                        select new
                        {
                            ParamName = t.Attribute("ParamName").Value,
                            ParamDesc = t.Attribute("ParamDesc").Value
                        };
                
                var g = from t in x.Root.XPathSelectElements("GlobalParams/Parameter")
                        select new
                        {
                            ParamName = t.Attribute("ParamName").Value,
                            ParamDesc = t.Attribute("ParamDesc").Value
                        };
                
                lvMailParam.DataSource = q.Concat(g);
                lvMailParam.DataBind();
            }
        }
    }
}
